package com.hanhai.material.device.utils.kingdee;

import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.result.KingdeeResult;
import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysBadRecord;
import com.hanhai.material.device.entity.SysProductInto;
import com.hanhai.material.device.entity.SysProductRecordInto;
import com.hanhai.material.device.entity.SysWorkPlan;
import com.hanhai.material.device.model.TokenModel;
import com.hanhai.material.device.model.product.*;
import com.hanhai.material.device.service.SysBadRecordService;
import com.hanhai.material.device.service.SysProductRecordIntoService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import static com.hanhai.material.device.entity.table.SysBadRecordTableDef.SYS_BAD_RECORD;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.mybatisflex.core.query.QueryMethods.sum;

/**
 * @author Administrator
 */
@Component
@Log4j2
public class ProductReceiptUtil {
    @Value("${kingdee.baseUrl}")
    String baseUrl;
    @Value("${kingdee.authorityCode}")
    String authorityCode;

    @Autowired
    SysProductRecordIntoService sysProductRecordIntoService;

    @Autowired
    KingDeleteUtil kingDeleteUtil;

    @Autowired
    SysBadRecordService sysBadRecordService;

    public String createToken() throws IOException {
        String url = baseUrl + "/Token/Create?authorityCode=" + authorityCode;
        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object, Object> map = new HashMap<>();
        HttpEntity<Map<Object, Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
        TokenModel tokenModel = jsonObject.getObject("Data", TokenModel.class);
        return tokenModel.getToken();
    }

    public Result<?> addAndUpdateProductReceipt(SysProductRecordInto productRecordInto) throws IOException {
        if (productRecordInto.getKingGoodNo() == null) {
            log.warn("良品入");
            if (productRecordInto.getGoodNumPeople() > 0 || productRecordInto.getLastTailCountInto() > 0) {
                ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, 2);
                KingdeeResult kingdeeResult = this.add(productReceiptData);
                if (kingdeeResult.getStatusCode() == 200) {
                    JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                    try {
                        productRecordInto.setKingGoodNo(jsonObject.getString("BillNo"));
                        productRecordInto.setKingGoodId(jsonObject.getString("BillInterID"));
                    } catch (Exception e) {
                    }
                }
            }
        } else {

            UpdateProductReceiptData updateProductReceiptData = this.getUpdateProductReceipt(productRecordInto, 2);
            if (productRecordInto.getGoodNumPeople() <= 0 && productRecordInto.getLastTailCountInto() <= 0 && productRecordInto.getKingBadNo() != null) {
                log.error("良品更新为零，删除");
                this.kingDeleteUtil.deleteProductReceiptByNo(productRecordInto.getKingBadNo());
                productRecordInto.setKingGoodNo(null);
            } else {
                log.warn("良品更新");
                KingdeeResult kingdeeResult = this.update(updateProductReceiptData);
                if (kingdeeResult.getStatusCode() == 200) {
                    JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                    log.error("JSONOBject:{}", jsonObject);
                    try {
                        productRecordInto.setKingGoodNo(jsonObject.getString("BillNo"));
                        productRecordInto.setKingGoodId(jsonObject.getString("BillInterID"));
                    } catch (Exception e) {

                    }
                } else if (kingdeeResult.getStatusCode() == 1402) {
                    log.error("无单据良品入");
                    ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, 2);
                    KingdeeResult kingdeeResult1 = this.add(productReceiptData);
                    if (kingdeeResult1.getStatusCode() == 200) {
                        JSONObject jsonObject = JSONObject.parseObject(kingdeeResult1.getData());
                        try {
                            productRecordInto.setKingGoodNo(jsonObject.getString("BillNo"));
                            productRecordInto.setKingGoodId(jsonObject.getString("BillInterID"));
                        } catch (Exception e) {

                        }
                    }
                }
            }

        }

        if (productRecordInto.getKingBadNo() == null) {
            if (productRecordInto.getBadNumPeople() > 0) {
                log.warn("不良品入");

                ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, 0);
                KingdeeResult kingdeeResult = this.add(productReceiptData);
                if (kingdeeResult.getStatusCode() == 200) {
                    JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                    log.error("JSONOBject:{}", jsonObject);
                    try {
                        productRecordInto.setKingBadNo(jsonObject.getString("BillNo"));
                        productRecordInto.setKingBadId(jsonObject.getString("BillInterID"));
                    } catch (Exception e) {

                    }
                }
            }
        } else {
            UpdateProductReceiptData updateProductReceiptData = this.getUpdateProductReceipt(productRecordInto, 0);
            if (productRecordInto.getBadNumPeople() <= 0 && productRecordInto.getKingBadNo() != null) {
                log.error("不良品入为零，删除");
                this.kingDeleteUtil.deleteProductReceiptByNo(productRecordInto.getKingBadNo());
                productRecordInto.setKingBadNo(null);
            } else {
                log.error("不良品入");
                KingdeeResult kingdeeResult = this.update(updateProductReceiptData);
                if (kingdeeResult.getStatusCode() == 200) {
                    JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                    log.error("JSONOBject:{}", jsonObject);
                    try {
                        productRecordInto.setKingBadNo(jsonObject.getString("BillNo"));
                        productRecordInto.setKingBadId(jsonObject.getString("BillInterID"));
                    } catch (Exception e) {

                    }
                } else if (kingdeeResult.getStatusCode() == 1402) {
                    log.error("无单据报废入");
                    ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, 0);
                    KingdeeResult kingdeeResult1 = this.add(productReceiptData);
                    if (kingdeeResult1.getStatusCode() == 200) {
                        JSONObject jsonObject = JSONObject.parseObject(kingdeeResult1.getData());
                        log.error("JSONOBject:{}", jsonObject);
                        try {
                            productRecordInto.setKingBadNo(jsonObject.getString("BillNo"));
                            productRecordInto.setKingBadId(jsonObject.getString("BillInterID"));
                        } catch (Exception e) {

                        }
                    }
                }
            }
        }
        if (productRecordInto.getKingBadBmNo() == null) {
            if (productRecordInto.getBadNumPeople() > 0) {
                log.warn("薄膜不良品入");
                ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, -1);
                if (!productReceiptData.getData().getPage2().isEmpty()) {
                    int qty = Integer.parseInt(productReceiptData.getData().getPage2().get(0).getFQty());
                    if (qty > 0) {
                        KingdeeResult kingdeeResult = this.add(productReceiptData);
                        if (kingdeeResult.getStatusCode() == 200) {
                            JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                            log.error("JSONObject:{}", jsonObject);
                            try {
                                productRecordInto.setKingBadBmNo(jsonObject.getString("BillNo"));
                                productRecordInto.setKingBadBmId(jsonObject.getString("BillInterID"));
                            } catch (Exception e) {

                            }
                        }
                    }

                }

            }
        } else {
            UpdateProductReceiptData updateProductReceiptData = this.getUpdateProductReceipt(productRecordInto, -1);

            if (productRecordInto.getBadNumPeople() <= 0 && productRecordInto.getKingBadBmNo() != null) {
                log.error("不良品入为零，删除");
                this.kingDeleteUtil.deleteProductReceiptByNo(productRecordInto.getKingBadBmNo());
                productRecordInto.setKingBadBmNo(null);
            } else {

                log.error("播不良品入");
                if (!updateProductReceiptData.getData().getPage2().isEmpty()) {
                    int qty = Integer.parseInt(updateProductReceiptData.getData().getPage2().get(0).getFQty());
                    if (qty > 0) {
                        KingdeeResult kingdeeResult = this.update(updateProductReceiptData);
                        if (kingdeeResult.getStatusCode() == 200) {
                            JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                            log.error("JSONOBject:{}", jsonObject);
                            try {
                                productRecordInto.setKingBadBmNo(jsonObject.getString("BillNo"));
                                productRecordInto.setKingBadBmId(jsonObject.getString("BillInterID"));
                            } catch (Exception e) {

                            }
                        } else if (kingdeeResult.getStatusCode() == 1402) {
                            log.error("无单据报废入");
                            ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, -1);
                            KingdeeResult kingdeeResult1 = this.add(productReceiptData);
                            if (kingdeeResult1.getStatusCode() == 200) {
                                JSONObject jsonObject = JSONObject.parseObject(kingdeeResult1.getData());
                                log.error("JSONOBject:{}", jsonObject);
                                try {
                                    productRecordInto.setKingBadBmNo(jsonObject.getString("BillNo"));
                                    productRecordInto.setKingBadBmId(jsonObject.getString("BillInterID"));
                                } catch (Exception e) {

                                }
                            }
                        }
                    }
                }
            }
        }
        int total = productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum();
        if (productRecordInto.getKingDetentionNo() == null) {
            if (total > 0) {
                log.warn("待判入");
                ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, 1);
                KingdeeResult kingdeeResult = this.add(productReceiptData);
                if (kingdeeResult.getStatusCode() == 200) {
                    JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                    log.error("JSONOBject:{}", jsonObject);
                    try {
                        productRecordInto.setKingDetentionNo(jsonObject.getString("BillNo"));
                        productRecordInto.setKingDetentionId(jsonObject.getString("BillInterID"));
                    } catch (Exception e) {

                    }
                }
            }

        } else {
            UpdateProductReceiptData updateProductReceiptData = this.getUpdateProductReceipt(productRecordInto, 1);
            if (total <= 0 && productRecordInto.getKingBadNo() != null) {
                log.error("待判入为零，删除");
                this.kingDeleteUtil.deleteProductReceiptByNo(productRecordInto.getKingDetentionNo());
                productRecordInto.setKingDetentionNo(null);
            } else {
                log.warn("待判更新");
                KingdeeResult kingdeeResult = this.update(updateProductReceiptData);
                if (kingdeeResult.getStatusCode() == 200) {
                    JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
                    log.error("JSONOBject:{}", jsonObject);
                    try {
                        productRecordInto.setKingDetentionNo(jsonObject.getString("BillNo"));
                        productRecordInto.setKingDetentionId(jsonObject.getString("BillInterID"));
                    } catch (Exception e) {

                    }
                } else if (kingdeeResult.getStatusCode() == 1402) {
                    log.warn("无单据待判入");
                    ProductReceiptData productReceiptData = this.getProductReceiptData(productRecordInto, 1);
                    KingdeeResult kingdeeResult1 = this.add(productReceiptData);
                    if (kingdeeResult1.getStatusCode() == 200) {
                        JSONObject jsonObject = JSONObject.parseObject(kingdeeResult1.getData());
                        log.error("JSONOBject:{}", jsonObject);
                        try {
                            productRecordInto.setKingDetentionNo(jsonObject.getString("BillNo"));
                            productRecordInto.setKingDetentionId(jsonObject.getString("BillInterID"));
                        } catch (Exception e) {

                        }
                    }
                }
            }
        }
        boolean result = this.sysProductRecordIntoService.updateById(productRecordInto);
        return result ? ResultTool.success() : ResultTool.fail();
    }

    public Result<?> addAndUpdateProductReceiptBatch(List<SysProductRecordInto> productRecordIntoList) {

        return ResultTool.fail();
    }

    public KingdeeResult add(ProductReceiptData productReceiptData) throws IOException {

        String token = this.createToken();
        String url = baseUrl + "/ProductReceipt/Save?token=" + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<ProductReceiptData> httpEntity = new HttpEntity<>(productReceiptData, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
        log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
        return JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
    }

    public KingdeeResult update(UpdateProductReceiptData updateProductReceiptData) throws IOException {
        String token = this.createToken();
        String url = baseUrl + "/ProductReceipt/Update?token=" + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<UpdateProductReceiptData> httpEntity = new HttpEntity<>(updateProductReceiptData, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
        log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
        return JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
    }

    public KingdeeResult remove(String billNo) throws IOException {
        String token = this.createToken();
        String url = baseUrl + "/ProductReceipt/Delete?token=" + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object,Object> map=new HashMap<>();
        map.put("Data", MapBuilder.create().put("FBillNo",billNo).build());
        HttpEntity<Map<Object,Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
        log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
        return JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
    }

    public KingdeeResult check(SysProductInto productInto) throws IOException {
        String token = this.createToken();
        String url = baseUrl + "/ProductReceipt/CheckBill?token=" + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object,Object> map=new HashMap<>();
        map.put("Data", MapBuilder.create().put("FBillNo",productInto.getKingNo()).put("FChecker",productInto.getCheckerId()).put("FCheckDirection","1").build());
        HttpEntity<Map<Object,Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
        log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
        return JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
    }


    public ProductReceiptData getProductReceiptData(SysProductRecordInto productRecordInto, Integer type) throws IOException {
        if (productRecordInto != null) {
            log.info("ProductRecordInto:{}", JSONObject.toJSONString(productRecordInto));
            SysWorkPlan workPlan = productRecordInto.getWorkPlan();

            ProductReceiptData productReceiptData = new ProductReceiptData();
            Data data = new Data();

            /****************************Page1开始*******************************/
            Page1 page1 = new Page1();

            /*交货单位*/
            FDeptID fDeptID = new FDeptID();
            fDeptID.setFNumber(workPlan.getWorkShopId());
            fDeptID.setFName(workPlan.getWorkShop());
            page1.setFDeptID(fDeptID);

            /*红蓝单标志*/
            page1.setFROB("1");
            /*日期*/
            page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productRecordInto.getProductDate()));

            FDCStockID fdcStockID = new FDCStockID();
            FFManagerID ffManagerID = new FFManagerID();
            FSManagerID fsManagerID = new FSManagerID();
            switch (type) {
                case -1: {
                    fdcStockID.setFNumber("WF5.BF6");
                    fdcStockID.setFName("WF5.BF6");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");
                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");
                    break;
                }
                case 0: {
                    fdcStockID.setFNumber("WF5.BF5");
                    fdcStockID.setFName("WF5.BF5");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");

                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");

                    break;
                }
                case 1: {
                    if(workPlan.getWorkPlanType().equals("普通订单")) {


                        fdcStockID.setFNumber("IN1.050");
                        fdcStockID.setFName("IN1.050");
                        ffManagerID.setFName("HJ03900");
                        ffManagerID.setFNumber("HJ03900");
                        fsManagerID.setFName("HJ03900");
                        fsManagerID.setFNumber("HJ03900");

                    }
                    break;

                }
                case 2: {
                    if(workPlan.getWorkPlanType().equals("普通订单")) {
                        String productId = productRecordInto.getProductId();
                        if (productId.startsWith("1.WC")) {
                            fdcStockID.setFNumber("X03.0");
                            fdcStockID.setFName("X03.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.WC")) {

                            fdcStockID.setFNumber("X03.4");
                            fdcStockID.setFName("X03.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                            fdcStockID.setFNumber("X01.0");
                            fdcStockID.setFName("X01.0");


                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.YD117")) {

                            fdcStockID.setFNumber("X01.4");
                            fdcStockID.setFName("X01.4");


                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("2.")) {
                            fdcStockID.setFNumber("X06.4");
                            fdcStockID.setFName("X06.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.")) {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        }
                    }else if(workPlan.getWorkPlanType().equals("试产")){
                        fdcStockID.setFNumber("X03.7");
                        fdcStockID.setFName("X03.7");
                        ffManagerID.setFName("HJ00312");
                        ffManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                        fsManagerID.setFNumber("HJ00312");
                    }
                    break;
                }
                default: {

                }
            }
            page1.setFFManagerID(ffManagerID);
            page1.setFSManagerID(fsManagerID);
            page1.setFDCStockID(fdcStockID);

            /*制单时间*/
            page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
            /*班别*/
            FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
            fHeadSelfA0233.setFName(productRecordInto.getShift());
            if (productRecordInto.getShiftCode().equals("A班")) {
                fHeadSelfA0233.setFNumber("1");
            } else {
                fHeadSelfA0233.setFNumber("2");
            }

            page1.setFHeadSelfA0233(fHeadSelfA0233);

            /*源单类型*/
            page1.setFSelTranType("生产任务单");

            List<Page1> page1List = new ArrayList<>();
            page1List.add(page1);
            /****************************Page1结束*******************************/


            /****************************Page2开始*******************************/
            Page2 page2 = new Page2();
            /*机台*/
            FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
            fEntrySelfA0244.setFNumber(productRecordInto.getMachineName());
            fEntrySelfA0244.setFName(productRecordInto.getMachineName());
            page2.setFEntrySelfA0244(fEntrySelfA0244);
            /*行号*/
            page2.setFEntryID2("1");
            /*物料编码*/
            FItemID fItemID = new FItemID();
            fItemID.setFNumber(productRecordInto.getProductId());
            fItemID.setFName("");
            page2.setFItemID(fItemID);

            /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

            /*单位*/
            FUnitID fUnitID = new FUnitID();
            fUnitID.setFNumber(workPlan.getUnitId());
            fUnitID.setFName(workPlan.getUnitName());
            page2.setFUnitID(fUnitID);

            /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));



            /*收货仓库*/
            FDCStockID1 fdcStockID1 = new FDCStockID1();
            FDCSPID fdcspid = new FDCSPID();
            String desp=productRecordInto.getGroupName()+productRecordInto.getShiftCode();

            switch (type) {
                case -1: {
                    fdcStockID1.setFNumber("WF5.BF6");
                    fdcStockID1.setFName("WF5.BF6");
                    fdcspid.setFName("921");
                    fdcspid.setFNumber("921");
                    page2.setFDCSPID(fdcspid);

                    QueryWrapper queryWrapper = QueryWrapper.create();
                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productRecordInto.getProductDate()));
                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.like("薄膜"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
                    for (SysBadRecord badRecord : badRecordList) {
                        num += badRecord.getNum();
//                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                        } else {
//                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                        }
                    }
                    //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));

                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 0: {
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");
                    fdcspid.setFName("987");
                    fdcspid.setFNumber("987");
                    page2.setFDCSPID(fdcspid);

                    QueryWrapper queryWrapper = QueryWrapper.create();
                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productRecordInto.getProductDate()));
                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.like("薄膜"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
                    for (SysBadRecord badRecord : badRecordList) {
                        num += badRecord.getNum();
//                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                        } else {
//                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                        }
                    }
                    //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));
                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 1: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
                    page2.setFauxqty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
                    page2.setFQty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
                    fdcStockID1.setFNumber("IN1.050");
                    fdcStockID1.setFName("成型待入仓");
                    fdcspid.setFName(desp);
                    fdcspid.setFNumber(desp);
                    page2.setFDCSPID(fdcspid);

                    break;
                }
                case 2: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));
                    page2.setFauxqty(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));
                    page2.setFQty(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));

                    fdcspid.setFName("977");
                    fdcspid.setFNumber("977");
                    page2.setFDCSPID(fdcspid);
                    if(workPlan.getWorkPlanType().equals("普通订单")) {
                        String productId = productRecordInto.getProductId();
                        if (productId.startsWith("1.WC")) {
                            fdcStockID1.setFNumber("X03.0");
                            fdcStockID1.setFName("X03.0");

                        } else if (productId.startsWith("2.WC")) {

                            fdcStockID1.setFNumber("X03.4");
                            fdcStockID1.setFName("X03.4");

                        } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                            fdcStockID1.setFNumber("X01.0");
                            fdcStockID1.setFName("X01.0");

                        } else if (productId.startsWith("2.YD11")) {

                            fdcStockID1.setFNumber("X01.4");
                            fdcStockID1.setFName("X01.4");

                        } else if (productId.startsWith("2.")) {
                            fdcStockID1.setFNumber("X06.4");
                            fdcStockID1.setFName("X06.4");

                        } else if (productId.startsWith("1.")) {

                            fdcStockID1.setFNumber("X06.0");
                            fdcStockID1.setFName("X06.0");
                        } else {
                            fdcStockID1.setFNumber("X06.0");
                            fdcStockID1.setFName("X06.0");

                        }
                    }
                    else if(workPlan.getWorkPlanType().equals("试产")){
                    fdcStockID.setFNumber("X03.7");
                    fdcStockID.setFName("X03.7");
                    ffManagerID.setFName("HJ00312");
                    ffManagerID.setFNumber("HJ00312");
                    fsManagerID.setFName("HJ00312");
                    fsManagerID.setFNumber("HJ00312");
                }

                    try {
                        int handleNum = productRecordInto.getDetentionToGoodNum() + productRecordInto.getWillProcessedToGoodNum();
                        StringJoiner stringJoiner = new StringJoiner(";");
                        if (productRecordInto.getDetentionNum() > 0) {
                            stringJoiner.add(productRecordInto.getCreateUserName()+"扣留调拨入库" + productRecordInto.getDetentionNum() + "PCS");
                        } else if (productRecordInto.getWillProcessedToGoodNum() > 0) {
                            stringJoiner.add(productRecordInto.getCreateUserName()+"待加工调拨入库" + productRecordInto.getDetentionNum() + "PCS");
                        }
                        page2.setFnote(stringJoiner.toString());
                    } catch (Exception e) {

                    }

                    break;
                }
                default: {

                }
            }


            page2.setFDCStockID1(fdcStockID1);

            /*源单单号*/
            page2.setFSourceBillNo(workPlan.getWorkPlanId());
            /*生产任务单号*/
            page2.setFICMOBillNo(workPlan.getWorkPlanId());

            /*源单内码*/
            page2.setFSourceInterId(String.valueOf(workPlan.getInterId()));
            /*源单类型*/
            page2.setFSourceTranType(String.valueOf(workPlan.getTranTypeId()));
            /*任务单内码*/
            page2.setFICMOInterID(String.valueOf(workPlan.getInterId()));


            List<Page2> page2List = new ArrayList<>();
            page2List.add(page2);
            /****************************Page2结束*******************************/

            data.setPage1(page1List);
            data.setPage2(page2List);
            productReceiptData.setData(data);


            return productReceiptData;
        }
        return null;
    }


    public ProductReceiptData getProductReceiptDataList(List<SysProductRecordInto> productRecordIntoList, SysProductRecordInto productRecordInto, Integer type) throws IOException {
        if (!productRecordIntoList.isEmpty()) {
            if (type == 0) {

                List<SysProductRecordInto> productRecordIntoOverList = productRecordIntoList.stream().filter(item -> ((double) item.getBadNumPeople() / (item.getGoodNumPeople() + item.getBadNumPeople() + item.getNowTailBoxCount() + item.getWillProcessedNum() + item.getDetentionNum())) > 0.05).collect(Collectors.toList());
                if (!productRecordIntoOverList.isEmpty()) {
                    log.error("报废超百分之五：{},共有：{}条数据", JSONObject.toJSONString(productRecordIntoOverList), productRecordIntoOverList.size());
                    ProductReceiptData productReceiptData = this.getProductReceiptDataList1(productRecordIntoOverList, productRecordIntoOverList.get(0), 0);
                    KingdeeResult kingdeeResult = this.add(productReceiptData);
                    this.updateProductInto(productRecordIntoOverList, kingdeeResult, 0);
                } else {

                }
                List<SysProductRecordInto> productRecordIntoOverList1 = productRecordIntoList.stream().filter(item -> ((double) item.getBadNumPeople() / (item.getGoodNumPeople() + item.getBadNumPeople() + item.getNowTailBoxCount() + item.getWillProcessedNum() + item.getDetentionNum())) <= 0.05).collect(Collectors.toList());
                if(!productRecordIntoOverList1.isEmpty()){
                    log.error("报废超百分之五：{},共有：{}条数据", JSONObject.toJSONString(productRecordIntoOverList1), productRecordIntoOverList1.size());
                    ProductReceiptData productReceiptData = this.getProductReceiptDataList1(productRecordIntoOverList1, productRecordIntoOverList1.get(0), 0);
                    KingdeeResult kingdeeResult = this.add(productReceiptData);
                    this.updateProductInto(productRecordIntoOverList1, kingdeeResult, 0);
                }

            } else {

                log.info("ProductRecordInto:{}", JSONObject.toJSONString(productRecordInto));
                SysWorkPlan workPlan = productRecordInto.getWorkPlan();

                ProductReceiptData productReceiptData = new ProductReceiptData();
                Data data = new Data();

                /****************************Page1开始*******************************/
                Page1 page1 = new Page1();

                /*交货单位*/
                FDeptID fDeptID = new FDeptID();
                fDeptID.setFNumber(workPlan.getWorkShopId());
                fDeptID.setFName(workPlan.getWorkShop());
                page1.setFDeptID(fDeptID);

                /*红蓝单标志*/
                page1.setFROB("1");
                /*日期*/
                page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productRecordInto.getProductDate()));

                FDCStockID fdcStockID = new FDCStockID();
                FFManagerID ffManagerID = new FFManagerID();
                FSManagerID fsManagerID = new FSManagerID();
                switch (type) {
                    case -2:{
                        fdcStockID.setFNumber("IN1.057");
                        fdcStockID.setFName("成型品质报废仓");
                        ffManagerID.setFName("HJ03900");
                        ffManagerID.setFNumber("HJ03900");
                        fsManagerID.setFName("HJ03900");
                        fsManagerID.setFNumber("HJ03900");
                        break;
                    }
                    case -1: {
                        fdcStockID.setFNumber("WF5.BF6");
                        fdcStockID.setFName("WF5.BF6");
                        ffManagerID.setFName("HJ00177");
                        ffManagerID.setFNumber("HJ00177");
                        fsManagerID.setFName("HJ00177");
                        fsManagerID.setFNumber("HJ00177");
                        break;
                    }
                    case 0: {
                        fdcStockID.setFNumber("WF5.BF5");
                        fdcStockID.setFName("WF5.BF5");
                        ffManagerID.setFName("HJ00177");
                        ffManagerID.setFNumber("HJ00177");

                        fsManagerID.setFName("HJ00177");
                        fsManagerID.setFNumber("HJ00177");

                        break;
                    }
                    case 1: {
                        fdcStockID.setFNumber("IN1.050");
                        fdcStockID.setFName("IN1.050");
                        ffManagerID.setFName("HJ03900");
                        ffManagerID.setFNumber("HJ03900");
                        fsManagerID.setFName("HJ03900");
                        fsManagerID.setFNumber("HJ03900");
                        break;

                    }
                    case 2: {
                        if(workPlan.getWorkPlanType().equals("普通订单")) {
                            String productId = productRecordInto.getProductId();
                            if (productId.startsWith("1.WC")) {
                                fdcStockID.setFNumber("X03.0");
                                fdcStockID.setFName("X03.0");

                                ffManagerID.setFNumber("HJ00312");
                                ffManagerID.setFName("HJ00312");

                                fsManagerID.setFNumber("HJ00312");
                                fsManagerID.setFName("HJ00312");
                            } else if (productId.startsWith("2.WC")) {

                                fdcStockID.setFNumber("X03.4");
                                fdcStockID.setFName("X03.4");

                                ffManagerID.setFNumber("HJ00206");
                                ffManagerID.setFName("HJ00206");

                                fsManagerID.setFNumber("HJ00206");
                                fsManagerID.setFName("HJ00206");
                            } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                                fdcStockID.setFNumber("X01.0");
                                fdcStockID.setFName("X01.0");
                                ffManagerID.setFNumber("HJ00312");
                                ffManagerID.setFName("HJ00312");

                                fsManagerID.setFNumber("HJ00312");
                                fsManagerID.setFName("HJ00312");
                            } else if (productId.startsWith("2.YD117")) {

                                fdcStockID.setFNumber("X01.4");
                                fdcStockID.setFName("X01.4");


                                ffManagerID.setFNumber("HJ00206");
                                ffManagerID.setFName("HJ00206");

                                fsManagerID.setFNumber("HJ00206");
                                fsManagerID.setFName("HJ00206");
                            } else if (productId.startsWith("2.")) {
                                fdcStockID.setFNumber("X06.4");
                                fdcStockID.setFName("X06.4");

                                ffManagerID.setFNumber("HJ00206");
                                ffManagerID.setFName("HJ00206");

                                fsManagerID.setFNumber("HJ00206");
                                fsManagerID.setFName("HJ00206");
                            } else if (productId.startsWith("1.")) {

                                fdcStockID.setFNumber("X06.0");
                                fdcStockID.setFName("X06.0");

                                ffManagerID.setFNumber("HJ00312");
                                ffManagerID.setFName("HJ00312");

                                fsManagerID.setFNumber("HJ00312");
                                fsManagerID.setFName("HJ00312");
                            } else {

                                fdcStockID.setFNumber("X06.0");
                                fdcStockID.setFName("X06.0");

                                ffManagerID.setFNumber("HJ00312");
                                ffManagerID.setFName("HJ00312");

                                fsManagerID.setFNumber("HJ00312");
                                fsManagerID.setFName("HJ00312");
                            }
                        }else if(workPlan.getWorkPlanType().equals("试产")){
                            fdcStockID.setFNumber("X03.7");
                            fdcStockID.setFName("X03.7");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        }
                        break;
                    }

                    default: {

                    }
                }
                page1.setFFManagerID(ffManagerID);
                page1.setFSManagerID(fsManagerID);
                page1.setFDCStockID(fdcStockID);

                /*制单时间*/
                page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
                /*班别*/
                FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
                fHeadSelfA0233.setFName(productRecordInto.getShift());
                if (productRecordInto.getShiftCode().equals("A班")) {
                    fHeadSelfA0233.setFNumber("1");
                } else {
                    fHeadSelfA0233.setFNumber("2");
                }

                page1.setFHeadSelfA0233(fHeadSelfA0233);

                /*源单类型*/
                page1.setFSelTranType("生产任务单");

                List<Page1> page1List = new ArrayList<>();
                page1List.add(page1);
                /****************************Page1结束*******************************/


                /****************************Page2开始*******************************/

                List<Page2> page2List = new ArrayList<>();
                int row = 0;
                for (SysProductRecordInto sysProductRecordInto : productRecordIntoList) {
                    if (sysProductRecordInto.getKingBadBmNo() != null && type == -1) {
                        continue;
                    } else if (sysProductRecordInto.getKingBadNo() != null && type == 0) {
                        continue;
                    } else if (sysProductRecordInto.getKingDetentionNo() != null && type == 1) {
                        continue;
                    } else if (sysProductRecordInto.getKingGoodNo() != null && type == 2) {
                        continue;
                    }
//                    if (sysProductRecordInto.getKingGoodNo() == null && sysProductRecordInto.getKingBadNo() == null && sysProductRecordInto.getKingBadBmNo() == null && sysProductRecordInto.getKingDetentionNo() == null) {
                    log.error("Pages");
                    SysWorkPlan workPlan1 = sysProductRecordInto.getWorkPlan();
                    row++;
                    Page2 page2 = new Page2();
                    /*机台*/
                    FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
                    fEntrySelfA0244.setFNumber(sysProductRecordInto.getMachineName());
                    fEntrySelfA0244.setFName(sysProductRecordInto.getMachineName());
                    page2.setFEntrySelfA0244(fEntrySelfA0244);
                    /*行号*/
                    page2.setFEntryID2(String.valueOf(row));
                    /*物料编码*/
                    FItemID fItemID = new FItemID();
                    fItemID.setFNumber(sysProductRecordInto.getProductId());
                    fItemID.setFName("");
                    page2.setFItemID(fItemID);

                    /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

                    /*单位*/
                    FUnitID fUnitID = new FUnitID();
                    fUnitID.setFNumber(workPlan1.getUnitId());
                    fUnitID.setFName(workPlan1.getUnitName());
                    page2.setFUnitID(fUnitID);

                    /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));



                    /*收货仓库*/
                    FDCStockID1 fdcStockID1 = new FDCStockID1();
                    FDCSPID fdcspid = new FDCSPID();
                    String desp=sysProductRecordInto.getGroupName()+sysProductRecordInto.getShiftCode();

                    switch (type) {
                        case -2:{
                            fdcStockID1.setFNumber("IN1.057");
                            fdcStockID1.setFName("成型品质报废仓");

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));
                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.eq("品质领取"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME,SYS_BAD_RECORD.TYPE);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME,SYS_BAD_RECORD.TYPE);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                if(badRecord.getType()==null){
                                    badRecord.setType("不良品");
                                }
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getType()+badRecord.getReasonName() + badRecord.getNum() + "PCS");
                                } else {
                                    stringJoiner.add(badRecord.getType()+badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));

                            page2.setFEntrySelfA0247(workPlan1.getMachineName()+stringJoiner.toString());/**/
                            break;
                        }
                        case -1: {
                            fdcStockID1.setFNumber("WF5.BF6");
                            fdcStockID1.setFName("WF5.BF6");
                            fdcspid.setFName("921");
                            fdcspid.setFNumber("921");
                            page2.setFDCSPID(fdcspid);

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));

                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.like("薄膜"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                                } else {
                                    stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            //                   page2.setFAuxQtyMust(String.valueOf(num));
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));

                            page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                            break;
                        }
                        case 0: {
                            fdcStockID1.setFNumber("WF5.BF5");
                            fdcStockID1.setFName("报废仓");
                            fdcspid.setFName("977");
                            fdcspid.setFNumber("977");
                            page2.setFDCSPID(fdcspid);

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));

                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLike("薄膜"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                                } else {
                                    stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            //                   page2.setFAuxQtyMust(String.valueOf(num));
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));
                            page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                            break;
                        }
                        case 1: {
                            //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            page2.setFauxqty(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            page2.setFQty(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            fdcStockID1.setFNumber("IN1.050");
                            fdcStockID1.setFName("成型待入仓");
                            fdcspid.setFName(desp);
                            fdcspid.setFNumber(desp);
                            page2.setFDCSPID(fdcspid);

                            break;
                        }
                        case 2: {
                            //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));
                            page2.setFauxqty(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));
                            page2.setFQty(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));

                            fdcspid.setFName("977");
                            fdcspid.setFNumber("977");
                            page2.setFDCSPID(fdcspid);
                            if(workPlan1.getWorkPlanType().equals("普通订单")) {
                                String productId = sysProductRecordInto.getProductId();
                                if (productId.startsWith("1.WC")) {
                                    fdcStockID1.setFNumber("X03.0");
                                    fdcStockID1.setFName("X03.0");

                                } else if (productId.startsWith("2.WC")) {

                                    fdcStockID1.setFNumber("X03.4");
                                    fdcStockID1.setFName("X03.4");

                                } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                                    fdcStockID1.setFNumber("X01.0");
                                    fdcStockID1.setFName("X01.0");

                                } else if (productId.startsWith("2.YD117")) {

                                    fdcStockID1.setFNumber("X01.4");
                                    fdcStockID1.setFName("X01.4");

                                } else if (productId.startsWith("2.")) {
                                    fdcStockID1.setFNumber("X06.4");
                                    fdcStockID1.setFName("X06.4");

                                } else if (productId.startsWith("1.")) {

                                    fdcStockID1.setFNumber("X06.0");
                                    fdcStockID1.setFName("X06.0");
                                } else {
                                    fdcStockID1.setFNumber("X06.0");
                                    fdcStockID1.setFName("X06.0");

                                }
                            }else if(workPlan.getWorkPlanType().equals("试产")){
                                fdcStockID1.setFNumber("X03.7");
                                fdcStockID1.setFName("X03.7");
                            }
                            try {
                                int handleNum = sysProductRecordInto.getDetentionToGoodNum() + sysProductRecordInto.getWillProcessedToGoodNum();
                                StringJoiner stringJoiner = new StringJoiner(";");
                                if (sysProductRecordInto.getDetentionToGoodNum() > 0) {
                                    stringJoiner.add(sysProductRecordInto.getCreateUserName()+"扣留调拨入库" + sysProductRecordInto.getDetentionToGoodNum() + "PCS");
                                } else if (sysProductRecordInto.getWillProcessedToGoodNum() > 0) {
                                    stringJoiner.add(sysProductRecordInto.getCreateUserName()+"待加工调拨入库" + sysProductRecordInto.getWillProcessedToGoodNum() + "PCS");
                                }
                                page2.setFnote(stringJoiner.toString());
                            } catch (Exception e) {

                            }


                            break;
                        }
                        default: {

                        }
                    }
                    try {
                        int total = productRecordInto.getGoodNumPeople() + productRecordInto.getBadNumPeople() + productRecordInto.getNowTailBoxCount() + productRecordInto.getWillProcessedNum() + productRecordInto.getDetentionNum();
                        double badRate = (double) productRecordInto.getBadNumPeople() / total;
                        page2.setFEntrySelfA0245(String.valueOf(badRate));
                    } catch (Exception e) {

                    }

                    page2.setFDCStockID1(fdcStockID1);

                    /*源单单号*/
                    page2.setFSourceBillNo(workPlan1.getWorkPlanId());
                    /*生产任务单号*/
                    page2.setFICMOBillNo(workPlan1.getWorkPlanId());

                    /*源单内码*/
                    page2.setFSourceInterId(String.valueOf(workPlan1.getInterId()));
                    /*源单类型*/
                    page2.setFSourceTranType(String.valueOf(workPlan1.getTranTypeId()));
                    /*任务单内码*/
                    page2.setFICMOInterID(String.valueOf(workPlan1.getInterId()));
                    int total = productRecordInto.getGoodNumPeople() + productRecordInto.getBadNumPeople() + productRecordInto.getNowTailBoxCount() + productRecordInto.getWillProcessedNum() + productRecordInto.getDetentionNum();
                    double rate = (double) productRecordInto.getBadNumPeople() / total;
                    page2.setFEntrySelfA0245(String.valueOf(rate));
                    if (Integer.parseInt(page2.getFQty()) > 0) {
                        page2List.add(page2);
                    }


//                    }
                    /****************************Page2结束*******************************/


                }


                data.setPage1(page1List);
                data.setPage2(page2List);
                productReceiptData.setData(data);
                return productReceiptData;
            }

        }
        return null;
    }

    public ProductReceiptData getProductReceiptDataList1(List<SysProductRecordInto> productRecordIntoList, SysProductRecordInto productRecordInto, Integer type) throws IOException {
        if (!productRecordIntoList.isEmpty()) {

            log.info("ProductRecordInto:{}", JSONObject.toJSONString(productRecordInto));
            SysWorkPlan workPlan = productRecordInto.getWorkPlan();

            ProductReceiptData productReceiptData = new ProductReceiptData();
            Data data = new Data();

            /****************************Page1开始*******************************/
            Page1 page1 = new Page1();

            /*交货单位*/
            FDeptID fDeptID = new FDeptID();
            fDeptID.setFNumber(workPlan.getWorkShopId());
            fDeptID.setFName(workPlan.getWorkShop());
            page1.setFDeptID(fDeptID);

            /*红蓝单标志*/
            page1.setFROB("1");
            /*日期*/
            page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productRecordInto.getProductDate()));

            FDCStockID fdcStockID = new FDCStockID();
            FFManagerID ffManagerID = new FFManagerID();
            FSManagerID fsManagerID = new FSManagerID();
            switch (type) {
                case -2:{
                    fdcStockID.setFNumber("IN1.057");
                    fdcStockID.setFName("成型品质报废仓");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    fsManagerID.setFName("HJ03900");
                    fsManagerID.setFNumber("HJ03900");
                    break;
                }
                case -1: {
                    fdcStockID.setFNumber("WF5.BF6");
                    fdcStockID.setFName("WF5.BF6");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");
                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");
                    break;
                }
                case 0: {
                    fdcStockID.setFNumber("WF5.BF5");
                    fdcStockID.setFName("WF5.BF5");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");

                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");

                    break;
                }
                case 1: {
                    fdcStockID.setFNumber("IN1.050");
                    fdcStockID.setFName("IN1.050");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    fsManagerID.setFName("HJ03900");
                    fsManagerID.setFNumber("HJ03900");
                    break;

                }
                case 2: {
                    String productId = productRecordInto.getProductId();
                    if (productId.startsWith("1.WC")) {
                        fdcStockID.setFNumber("X03.0");
                        fdcStockID.setFName("X03.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.WC")) {

                        fdcStockID.setFNumber("X03.4");
                        fdcStockID.setFName("X03.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                        fdcStockID.setFNumber("X01.0");
                        fdcStockID.setFName("X01.0");


                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID.setFNumber("X01.4");
                        fdcStockID.setFName("X01.4");


                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("2.")) {
                        fdcStockID.setFNumber("X06.4");
                        fdcStockID.setFName("X06.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.")) {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }

                    break;
                }
//                case 3:{
//                    fdcStockID.setFNumber("IN1.055");
//                    fdcStockID.setFName("IN1.055");
//                    ffManagerID.setFName("HJ03900");
//                    ffManagerID.setFNumber("HJ03900");
//                    fsManagerID.setFName("HJ03900");
//                    fsManagerID.setFNumber("HJ03900");
//                    break;
//                }
                default: {

                }
            }
            page1.setFFManagerID(ffManagerID);
            page1.setFSManagerID(fsManagerID);
            page1.setFDCStockID(fdcStockID);

            /*制单时间*/
            page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
            /*班别*/
            FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
            fHeadSelfA0233.setFName(productRecordInto.getShift());
            if (productRecordInto.getShiftCode().equals("A班")) {
                fHeadSelfA0233.setFNumber("1");
            } else {
                fHeadSelfA0233.setFNumber("2");
            }

            page1.setFHeadSelfA0233(fHeadSelfA0233);

            /*源单类型*/
            page1.setFSelTranType("生产任务单");

            List<Page1> page1List = new ArrayList<>();
            page1List.add(page1);
            /****************************Page1结束*******************************/


            /****************************Page2开始*******************************/

            List<Page2> page2List = new ArrayList<>();
            int row = 0;
            for (SysProductRecordInto sysProductRecordInto : productRecordIntoList) {
                if (sysProductRecordInto.getKingGoodNo() == null && sysProductRecordInto.getKingBadNo() == null && sysProductRecordInto.getKingBadBmNo() == null && sysProductRecordInto.getKingDetentionNo() == null) {
                    SysWorkPlan workPlan1 = sysProductRecordInto.getWorkPlan();
                    row++;
                    Page2 page2 = new Page2();
                    /*机台*/
                    FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
                    fEntrySelfA0244.setFNumber(sysProductRecordInto.getMachineName());
                    fEntrySelfA0244.setFName(sysProductRecordInto.getMachineName());
                    page2.setFEntrySelfA0244(fEntrySelfA0244);
                    /*行号*/
                    page2.setFEntryID2(String.valueOf(row));
                    /*物料编码*/
                    FItemID fItemID = new FItemID();
                    fItemID.setFNumber(sysProductRecordInto.getProductId());
                    fItemID.setFName("");
                    page2.setFItemID(fItemID);

                    /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

                    /*单位*/
                    FUnitID fUnitID = new FUnitID();
                    fUnitID.setFNumber(workPlan1.getUnitId());
                    fUnitID.setFName(workPlan1.getUnitName());
                    page2.setFUnitID(fUnitID);

                    /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));



                    /*收货仓库*/
                    FDCStockID1 fdcStockID1 = new FDCStockID1();
                    FDCSPID fdcspid = new FDCSPID();
                    String desp=sysProductRecordInto.getGroupName()+sysProductRecordInto.getShiftCode();
                    switch (type) {
                        case -2:{
                            fdcStockID1.setFNumber("IN1.057");
                            fdcStockID1.setFName("成型品质报废仓");

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));
                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.eq("品质领取"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME,SYS_BAD_RECORD.TYPE);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                if(badRecord.getType()==null){
                                    badRecord.setType("不良品");
                                }
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getType()+badRecord.getReasonName() + badRecord.getNum() + "PCS");
                                } else {
                                    stringJoiner.add(badRecord.getType()+badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));

                            page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                            break;
                        }
                        case -1: {
                            fdcStockID1.setFNumber("WF5.BF6");
                            fdcStockID1.setFName("WF5.BF6");
                            fdcspid.setFName("921");
                            fdcspid.setFNumber("921");
                            page2.setFDCSPID(fdcspid);

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));

                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.like("薄膜"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                                } else {
                                    stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            //                   page2.setFAuxQtyMust(String.valueOf(num));
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));

                            page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                            break;
                        }
                        case 0: {
                            fdcStockID1.setFNumber("WF5.BF5");
                            fdcStockID1.setFName("报废仓");
                            fdcspid.setFName("987");
                            fdcspid.setFNumber("987");
                            page2.setFDCSPID(fdcspid);

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));

                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLike("薄膜"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                                } else {
                                    stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            //                   page2.setFAuxQtyMust(String.valueOf(num));
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));
                            page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                            break;
                        }
                        case 1: {
                            //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            page2.setFauxqty(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            page2.setFQty(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            fdcStockID1.setFNumber("IN1.050");
                            fdcStockID1.setFName("成型待入仓");
                            fdcspid.setFName(desp);
                            fdcspid.setFNumber(desp);
                            page2.setFDCSPID(fdcspid);

                            break;
                        }
                        case 2: {
                            //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));
                            page2.setFauxqty(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));
                            page2.setFQty(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));

                            fdcspid.setFName("977");
                            fdcspid.setFNumber("977");
                            page2.setFDCSPID(fdcspid);

                            if(workPlan1.getWorkPlanType().equals("普通订单")) {
                                String productId = sysProductRecordInto.getProductId();
                                if (productId.startsWith("1.WC")) {
                                    fdcStockID1.setFNumber("X03.0");
                                    fdcStockID1.setFName("X03.0");

                                } else if (productId.startsWith("2.WC")) {

                                    fdcStockID1.setFNumber("X03.4");
                                    fdcStockID1.setFName("X03.4");

                                } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                                    fdcStockID1.setFNumber("X01.0");
                                    fdcStockID1.setFName("X01.0");

                                } else if (productId.startsWith("2.YD117")) {

                                    fdcStockID1.setFNumber("X01.4");
                                    fdcStockID1.setFName("X01.4");

                                } else if (productId.startsWith("2.")) {
                                    fdcStockID1.setFNumber("X06.4");
                                    fdcStockID1.setFName("X06.4");

                                } else if (productId.startsWith("1.")) {

                                    fdcStockID1.setFNumber("X06.0");
                                    fdcStockID1.setFName("X06.0");
                                } else {
                                    fdcStockID1.setFNumber("X06.0");
                                    fdcStockID1.setFName("X06.0");

                                }
                            }else if(workPlan.getWorkPlanType().equals("试产")){
                                fdcStockID1.setFNumber("X03.7");
                                fdcStockID1.setFName("X03.7");
                            }
                            try {
                                int handleNum = sysProductRecordInto.getDetentionToGoodNum() + sysProductRecordInto.getWillProcessedToGoodNum();
                                StringJoiner stringJoiner = new StringJoiner(";");
                                if (sysProductRecordInto.getDetentionToGoodNum() > 0) {
                                    stringJoiner.add(sysProductRecordInto.getCreateUserName()+"扣留调拨入库" + sysProductRecordInto.getDetentionToGoodNum() + "PCS");
                                } else if (sysProductRecordInto.getWillProcessedToGoodNum() > 0) {
                                    stringJoiner.add(sysProductRecordInto.getCreateUserName()+"待加工调拨入库" + sysProductRecordInto.getWillProcessedToGoodNum() + "PCS");
                                }
                                page2.setFnote(stringJoiner.toString());
                            } catch (Exception e) {

                            }

                            break;
                        }
//                        case 3:{
//                            fdcStockID.setFNumber("IN1.055");
//                            fdcStockID.setFName("IN1.055");
//                            ffManagerID.setFName("HJ03900");
//                            ffManagerID.setFNumber("HJ03900");
//                            fsManagerID.setFName("HJ03900");
//                            fsManagerID.setFNumber("HJ03900");
//                            break;
//                        }
                        default: {

                        }
                    }


                    page2.setFDCStockID1(fdcStockID1);

                    /*源单单号*/
                    page2.setFSourceBillNo(workPlan1.getWorkPlanId());
                    /*生产任务单号*/
                    page2.setFICMOBillNo(workPlan1.getWorkPlanId());

                    /*源单内码*/
                    page2.setFSourceInterId(String.valueOf(workPlan1.getInterId()));
                    /*源单类型*/
                    page2.setFSourceTranType(String.valueOf(workPlan1.getTranTypeId()));
                    /*任务单内码*/
                    page2.setFICMOInterID(String.valueOf(workPlan1.getInterId()));
                    int total = productRecordInto.getGoodNumPeople() + productRecordInto.getBadNumPeople() + productRecordInto.getNowTailBoxCount() + productRecordInto.getWillProcessedNum() + productRecordInto.getDetentionNum();
                    double rate = (double) productRecordInto.getBadNumPeople() / total;
                    page2.setFEntrySelfA0245(String.valueOf(rate));
                    if (Integer.parseInt(page2.getFQty()) > 0) {
                        page2List.add(page2);
                    }

                }
                /****************************Page2结束*******************************/


            }
            data.setPage1(page1List);
            data.setPage2(page2List);
            productReceiptData.setData(data);
            return productReceiptData;
        }
        return null;
    }

    public ProductReceiptData getProductReceiptDataList2(List<SysProductRecordInto> productRecordIntoList, SysProductRecordInto productRecordInto, Integer type) throws IOException {
        if (!productRecordIntoList.isEmpty()) {

            log.info("ProductRecordInto:{}", JSONObject.toJSONString(productRecordInto));
            SysWorkPlan workPlan = productRecordInto.getWorkPlan();

            ProductReceiptData productReceiptData = new ProductReceiptData();
            Data data = new Data();

            /****************************Page1开始*******************************/
            Page1 page1 = new Page1();

            /*交货单位*/
            FDeptID fDeptID = new FDeptID();
            fDeptID.setFNumber(workPlan.getWorkShopId());
            fDeptID.setFName(workPlan.getWorkShop());
            page1.setFDeptID(fDeptID);

            /*红蓝单标志*/
            page1.setFROB("1");
            /*日期*/
            page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productRecordInto.getProductDate()));

            FDCStockID fdcStockID = new FDCStockID();
            FFManagerID ffManagerID = new FFManagerID();
            FSManagerID fsManagerID = new FSManagerID();
            switch (type) {
                case -1: {
                    fdcStockID.setFNumber("WF5.BF6");
                    fdcStockID.setFName("WF5.BF6");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");
                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");
                    break;
                }
                case 0: {
                    fdcStockID.setFNumber("WF5.BF5");
                    fdcStockID.setFName("WF5.BF5");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");

                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");

                    break;
                }
                case 1: {
                    fdcStockID.setFNumber("IN1.050");
                    fdcStockID.setFName("IN1.050");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    fsManagerID.setFName("HJ03900");
                    fsManagerID.setFNumber("HJ03900");
                    break;

                }
                case 2: {
                    if(workPlan.getWorkPlanType().equals("普通订单")) {

                        String productId = productRecordInto.getProductId();
                        if (productId.startsWith("1.WC")) {
                            fdcStockID.setFNumber("X03.0");
                            fdcStockID.setFName("X03.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.WC")) {

                            fdcStockID.setFNumber("X03.4");
                            fdcStockID.setFName("X03.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                            fdcStockID.setFNumber("X01.0");
                            fdcStockID.setFName("X01.0");


                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.YD117")) {

                            fdcStockID.setFNumber("X01.4");
                            fdcStockID.setFName("X01.4");


                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("2.")) {
                            fdcStockID.setFNumber("X06.4");
                            fdcStockID.setFName("X06.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.")) {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        }
                    }else if(workPlan.getWorkPlanType().equals("试产")){
                        fdcStockID.setFNumber("X03.7");
                        fdcStockID.setFName("X03.7");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }

                    break;
                }
                default: {

                }
            }
            page1.setFFManagerID(ffManagerID);
            page1.setFSManagerID(fsManagerID);
            page1.setFDCStockID(fdcStockID);

            /*制单时间*/
            page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
            /*班别*/
            FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
            fHeadSelfA0233.setFName(productRecordInto.getShift());
            if (productRecordInto.getShiftCode().equals("A班")) {
                fHeadSelfA0233.setFNumber("1");
            } else {
                fHeadSelfA0233.setFNumber("2");
            }

            page1.setFHeadSelfA0233(fHeadSelfA0233);

            /*源单类型*/
            page1.setFSelTranType("生产任务单");

            List<Page1> page1List = new ArrayList<>();
            page1List.add(page1);
            /****************************Page1结束*******************************/


            /****************************Page2开始*******************************/

            List<Page2> page2List = new ArrayList<>();
            int row = 0;
            for (SysProductRecordInto sysProductRecordInto : productRecordIntoList) {
                if (sysProductRecordInto.getKingGoodNo() == null && sysProductRecordInto.getKingBadNo() == null && sysProductRecordInto.getKingBadBmNo() == null && sysProductRecordInto.getKingDetentionNo() == null) {
                    SysWorkPlan workPlan1 = sysProductRecordInto.getWorkPlan();
                    row++;
                    Page2 page2 = new Page2();
                    /*机台*/
                    FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
                    fEntrySelfA0244.setFNumber(sysProductRecordInto.getMachineName());
                    fEntrySelfA0244.setFName(sysProductRecordInto.getMachineName());
                    page2.setFEntrySelfA0244(fEntrySelfA0244);
                    /*行号*/
                    page2.setFEntryID2(String.valueOf(row));
                    /*物料编码*/
                    FItemID fItemID = new FItemID();
                    fItemID.setFNumber(sysProductRecordInto.getProductId());
                    fItemID.setFName("");
                    page2.setFItemID(fItemID);

                    /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

                    /*单位*/
                    FUnitID fUnitID = new FUnitID();
                    fUnitID.setFNumber(workPlan1.getUnitId());
                    fUnitID.setFName(workPlan1.getUnitName());
                    page2.setFUnitID(fUnitID);

                    /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));



                    /*收货仓库*/
                    FDCStockID1 fdcStockID1 = new FDCStockID1();
                    FDCSPID fdcspid = new FDCSPID();
                    String desp=sysProductRecordInto.getGroupName()+sysProductRecordInto.getShiftCode();
                    switch (type) {
                        case -1: {
                            fdcStockID1.setFNumber("WF5.BF6");
                            fdcStockID1.setFName("WF5.BF6");
                            fdcspid.setFName("921");
                            fdcspid.setFNumber("921");
                            page2.setFDCSPID(fdcspid);

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));

                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.like("薄膜"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                                } else {
                                    stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            //                   page2.setFAuxQtyMust(String.valueOf(num));
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));

                            page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                            break;
                        }
                        case 0: {
                            fdcStockID1.setFNumber("WF5.BF5");
                            fdcStockID1.setFName("报废仓");
                            fdcspid.setFName("987");
                            fdcspid.setFNumber("987");
                            page2.setFDCSPID(fdcspid);

                            QueryWrapper queryWrapper = QueryWrapper.create();
                            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordInto.getProductDate())));

                            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysProductRecordInto.getShift()));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLike("薄膜"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                            queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                            List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                            StringJoiner stringJoiner = new StringJoiner(";");
                            int num = 0;
                            for (SysBadRecord badRecord : badRecordList) {
                                num += badRecord.getNum();
                                if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                                    stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                                } else {
                                    stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                                }
                            }
                            //                   page2.setFAuxQtyMust(String.valueOf(num));
                            page2.setFauxqty(String.valueOf(num));
                            page2.setFQty(String.valueOf(num));
                            page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                            break;
                        }
                        case 1: {
                            //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            page2.setFauxqty(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            page2.setFQty(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
                            fdcStockID1.setFNumber("IN1.050");
                            fdcStockID1.setFName("成型待入仓");
                            fdcspid.setFName(desp);
                            fdcspid.setFNumber(desp);
                            page2.setFDCSPID(fdcspid);

                            break;
                        }
                        case 2: {
                            //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));
                            page2.setFauxqty(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));
                            page2.setFQty(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));

                            fdcspid.setFName("977");
                            fdcspid.setFNumber("977");
                            page2.setFDCSPID(fdcspid);

                            if(workPlan1.getWorkPlanType().equals("普通订单")) {
                                String productId = sysProductRecordInto.getProductId();
                                if (productId.startsWith("1.WC")) {
                                    fdcStockID1.setFNumber("X03.0");
                                    fdcStockID1.setFName("X03.0");

                                } else if (productId.startsWith("2.WC")) {

                                    fdcStockID1.setFNumber("X03.4");
                                    fdcStockID1.setFName("X03.4");

                                } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                                    fdcStockID1.setFNumber("X01.0");
                                    fdcStockID1.setFName("X01.0");

                                } else if (productId.startsWith("2.YD117")) {

                                    fdcStockID1.setFNumber("X01.4");
                                    fdcStockID1.setFName("X01.4");

                                } else if (productId.startsWith("2.")) {
                                    fdcStockID1.setFNumber("X06.4");
                                    fdcStockID1.setFName("X06.4");

                                } else if (productId.startsWith("1.")) {

                                    fdcStockID1.setFNumber("X06.0");
                                    fdcStockID1.setFName("X06.0");
                                } else {
                                    fdcStockID1.setFNumber("X06.0");
                                    fdcStockID1.setFName("X06.0");

                                }
                            }else{
                                fdcStockID1.setFNumber("X03.0");
                                fdcStockID1.setFName("X03.0");
                            }
                            try {
                                int handleNum = sysProductRecordInto.getDetentionToGoodNum() + sysProductRecordInto.getWillProcessedToGoodNum();
                                StringJoiner stringJoiner = new StringJoiner(";");
                                if (sysProductRecordInto.getDetentionToGoodNum() > 0) {
                                    stringJoiner.add("扣留调拨入库" + sysProductRecordInto.getDetentionToGoodNum() + "PCS");
                                } else if (sysProductRecordInto.getWillProcessedToGoodNum() > 0) {
                                    stringJoiner.add("待加工调拨入库" + sysProductRecordInto.getWillProcessedToGoodNum() + "PCS");
                                }
                                page2.setFnote(stringJoiner.toString());
                            } catch (Exception e) {

                            }

                            break;
                        }
                        default: {

                        }
                    }


                    page2.setFDCStockID1(fdcStockID1);

                    /*源单单号*/
                    page2.setFSourceBillNo(workPlan1.getWorkPlanId());
                    /*生产任务单号*/
                    page2.setFICMOBillNo(workPlan1.getWorkPlanId());

                    /*源单内码*/
                    page2.setFSourceInterId(String.valueOf(workPlan1.getInterId()));
                    /*源单类型*/
                    page2.setFSourceTranType(String.valueOf(workPlan1.getTranTypeId()));
                    /*任务单内码*/
                    page2.setFICMOInterID(String.valueOf(workPlan1.getInterId()));
                    int total = productRecordInto.getGoodNumPeople() + productRecordInto.getBadNumPeople() + productRecordInto.getNowTailBoxCount() + productRecordInto.getWillProcessedNum() + productRecordInto.getDetentionNum();
                    double rate = (double) productRecordInto.getBadNumPeople() / total;
                    page2.setFEntrySelfA0245(String.valueOf(rate));
                    if (Integer.parseInt(page2.getFQty()) > 0) {
                        page2List.add(page2);
                    }

                }
                /****************************Page2结束*******************************/


            }
            data.setPage1(page1List);
            data.setPage2(page2List);
            productReceiptData.setData(data);
            return productReceiptData;
        }
        return null;
    }


    public UpdateProductReceiptData getUpdateProductReceipt(SysProductRecordInto productRecordInto, Integer type) throws IOException {
        if (productRecordInto != null) {
            log.info("ProductRecordInto:{}", JSONObject.toJSONString(productRecordInto));
            SysWorkPlan workPlan = productRecordInto.getWorkPlan();

            UpdateProductReceiptData productReceiptData = new UpdateProductReceiptData();
            Data data = new Data();

            /****************************Page1开始*******************************/
            Page1 page1 = new Page1();

            /*交货单位*/
            FDeptID fDeptID = new FDeptID();
            fDeptID.setFNumber(workPlan.getWorkShopId());
            fDeptID.setFName(workPlan.getWorkShop());
            page1.setFDeptID(fDeptID);

            /*红蓝单标志*/
            page1.setFROB("1");
            /*日期*/
            page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productRecordInto.getProductDate()));
            FDCStockID fdcStockID = new FDCStockID();
            FFManagerID ffManagerID = new FFManagerID();
            FSManagerID fsManagerID = new FSManagerID();
            switch (type) {
                case -1: {
                    fdcStockID.setFNumber("WF5.BF6");
                    fdcStockID.setFName("WF5.BF6");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");
                    break;
                }
                case 0: {
                    fdcStockID.setFNumber("WF5.BF5");
                    fdcStockID.setFName("WF5.BF5");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");

                    break;
                }
                case 1: {
                    fdcStockID.setFNumber("IN1.050");
                    fdcStockID.setFName("IN1.050");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    break;

                }
                case 2: {
                    String productId = productRecordInto.getProductId();
                    if (productId.startsWith("1.WC")) {
                        fdcStockID.setFNumber("X03.0");
                        fdcStockID.setFName("X03.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.WC")) {

                        fdcStockID.setFNumber("X03.4");
                        fdcStockID.setFName("X03.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                        fdcStockID.setFNumber("X01.0");
                        fdcStockID.setFName("X01.0");


                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID.setFNumber("X01.4");
                        fdcStockID.setFName("X01.4");


                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("2.")) {
                        fdcStockID.setFNumber("X06.4");
                        fdcStockID.setFName("X06.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.")) {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }
                    page1.setFFManagerID(ffManagerID);
                    page1.setFSManagerID(fsManagerID);
                    page1.setFDCStockID(fdcStockID);
                    break;
                }
                default: {

                }
            }

            /*制单时间*/
            page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
            /*班别*/
            FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
            fHeadSelfA0233.setFName(productRecordInto.getShift());
            if (productRecordInto.getShiftCode().equals("A班")) {
                fHeadSelfA0233.setFNumber("1");
            } else {
                fHeadSelfA0233.setFNumber("2");
            }

            page1.setFHeadSelfA0233(fHeadSelfA0233);

            /*源单类型*/
            page1.setFSelTranType("生产任务单");

            List<Page1> page1List = new ArrayList<>();
            page1List.add(page1);
            /****************************Page1结束*******************************/


            /****************************Page2开始*******************************/
            Page2 page2 = new Page2();
            /*机台*/
            FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
            fEntrySelfA0244.setFNumber(productRecordInto.getMachineName());
            fEntrySelfA0244.setFName(productRecordInto.getMachineName());
            page2.setFEntrySelfA0244(fEntrySelfA0244);

            /*行号*/
            page2.setFEntryID2("1");
            /*物料编码*/
            FItemID fItemID = new FItemID();
            fItemID.setFNumber(productRecordInto.getProductId());
            fItemID.setFName("");
            page2.setFItemID(fItemID);

            /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

            /*单位*/
            FUnitID fUnitID = new FUnitID();
            fUnitID.setFNumber(workPlan.getUnitId());
            fUnitID.setFName(workPlan.getUnitName());
            page2.setFUnitID(fUnitID);

            /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));

            /*仓位*/
//            FDCSPID fdcspid=new FDCSPID();
//            fdcspid.setFName();
//            page2.setFDCSPID();

            /*收货仓库*/
            FDCStockID1 fdcStockID1 = new FDCStockID1();
            FDCSPID fdcspid = new FDCSPID();
            String desp=productRecordInto.getGroupName()+productRecordInto.getShiftCode();

            switch (type) {
                case -1: {
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");
                    fdcspid.setFName("987");
                    fdcspid.setFNumber("987");
                    page2.setFDCSPID(fdcspid);

                    QueryWrapper queryWrapper = QueryWrapper.create();
                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productRecordInto.getProductDate()));
                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLike("薄膜"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
                    for (SysBadRecord badRecord : badRecordList) {
                        num += badRecord.getNum();
                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                        } else {
                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                        }
                    }
// //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));
                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 0: {
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");
                    fdcspid.setFName("987");
                    fdcspid.setFNumber("987");
                    page2.setFDCSPID(fdcspid);

                    QueryWrapper queryWrapper = QueryWrapper.create();
                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productRecordInto.getProductDate()));
                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLike("薄膜"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
                    for (SysBadRecord badRecord : badRecordList) {
                        num += badRecord.getNum();
                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");

                        } else {
                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
                        }
                    }
                    //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));
                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 1: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
                    page2.setFauxqty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
                    page2.setFQty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
                    fdcStockID1.setFNumber("IN1.050");
                    fdcStockID1.setFName("成型待入仓");
                    fdcspid.setFName(desp);
                    fdcspid.setFNumber(desp);
                    page2.setFDCSPID(fdcspid);
                    break;
                }
                case 2: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));
                    page2.setFauxqty(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));
                    page2.setFQty(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));

                    fdcspid.setFName("977");
                    fdcspid.setFNumber("977");
                    page2.setFDCSPID(fdcspid);

                    String productId = productRecordInto.getProductId();
                    if (productId.startsWith("1.WC")) {
                        fdcStockID1.setFNumber("X03.0");
                        fdcStockID1.setFName("X03.0");

                    } else if (productId.startsWith("2.WC")) {

                        fdcStockID1.setFNumber("X03.4");
                        fdcStockID1.setFName("X03.4");

                    } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                        fdcStockID1.setFNumber("X01.0");
                        fdcStockID1.setFName("X01.0");

                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID1.setFNumber("X01.4");
                        fdcStockID1.setFName("X01.4");

                    } else if (productId.startsWith("2.")) {
                        fdcStockID1.setFNumber("X06.4");
                        fdcStockID1.setFName("X06.4");

                    } else if (productId.startsWith("1.")) {

                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");
                    } else {
                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");

                    }
                    try {
                        int handleNum = productRecordInto.getDetentionToGoodNum() + productRecordInto.getWillProcessedToGoodNum();
                        StringJoiner stringJoiner = new StringJoiner(";");
                        if (productRecordInto.getDetentionNum() > 0) {
                            stringJoiner.add(productRecordInto.getCreateUserName()+"扣留调拨入库" + productRecordInto.getDetentionNum() + "PCS");
                        } else if (productRecordInto.getWillProcessedToGoodNum() > 0) {
                            stringJoiner.add(productRecordInto.getCreateUserName()+"待加工调拨入库" + productRecordInto.getDetentionNum() + "PCS");
                        }
                        page2.setFnote(stringJoiner.toString());
                    } catch (Exception e) {

                    }

                    break;
                }
                default: {

                }
            }


            page2.setFDCStockID1(fdcStockID1);
            /*源单单号*/
            page2.setFSourceBillNo(workPlan.getWorkPlanId());
            /*生产任务单号*/
            page2.setFICMOBillNo(workPlan.getWorkPlanId());

            /*源单内码*/
            page2.setFSourceInterId(String.valueOf(workPlan.getInterId()));
            /*源单类型*/
            page2.setFSourceTranType(String.valueOf(workPlan.getTranTypeId()));
            /*任务单内码*/
            page2.setFICMOInterID(String.valueOf(workPlan.getInterId()));

            List<Page2> page2List = new ArrayList<>();
            page2List.add(page2);
            /****************************Page2结束*******************************/

            data.setPage1(page1List);
            data.setPage2(page2List);
            productReceiptData.setData(data);

            switch (type) {
                case -1: {
                    productReceiptData.setFBillNo(productRecordInto.getKingBadBmNo());
                    break;
                }
                case 0: {
                    productReceiptData.setFBillNo(productRecordInto.getKingBadNo());
                    break;
                }
                case 1: {
                    productReceiptData.setFBillNo(productRecordInto.getKingDetentionNo());
                    break;
                }
                case 2: {
                    productReceiptData.setFBillNo(productRecordInto.getKingGoodNo());
                    break;
                }
                default: {

                }
            }

            log.error("Data:{}", JSONObject.toJSONString(productReceiptData));
            return productReceiptData;
        }
        return null;
    }


    public void updateProductInto(List<SysProductRecordInto> productRecordIntoList, KingdeeResult kingdeeResult, Integer type) {
        if (kingdeeResult.getStatusCode() == 200) {
            JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
            log.error("JSONOBject:{}", jsonObject);
            try {
                for (SysProductRecordInto productRecordInto : productRecordIntoList) {
                    switch (type) {
                        case -1:
                            productRecordInto.setKingBadBmNo(jsonObject.getString("BillNo"));
                            productRecordInto.setKingBadBmId(jsonObject.getString("BillInterID"));
                            break;
                        case 0:
                            productRecordInto.setKingBadNo(jsonObject.getString("BillNo"));
                            productRecordInto.setKingBadId(jsonObject.getString("BillInterID"));
                            break;

                        case 1:
                            productRecordInto.setKingDetentionNo(jsonObject.getString("BillNo"));
                            productRecordInto.setKingDetentionId(jsonObject.getString("BillInterID"));
                            break;

                        case 2:
                            productRecordInto.setKingGoodNo(jsonObject.getString("BillNo"));
                            productRecordInto.setKingGoodId(jsonObject.getString("BillInterID"));
                            break;
                        default:

                            break;
                    }

                }
                this.sysProductRecordIntoService.updateBatch(productRecordIntoList);

            } catch (Exception e) {

            }
        }

    }

}
